<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\product\Cart;
use app\admin\model\product\Sku;
use app\admin\model\product\Value;
use app\common\controller\Api;
use think\Controller;
use think\Db;
use think\Request;


class Productcollect extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
//        self::$model = new Cart();
    }

    /**
     * 商品收藏列表
     */
    public function shopCollect()
    {
        $page = input('page');
        $start = ($page - 1) * self::$totalList;

        $uid = $this->uid;
        $collInfos = Db::table('zx_collection_product')->join('zx_shop shop', 'zx_collection_product.product_id = shop.id')
            ->field('zx_collection_product.id,zx_collection_product.product_id,shop.goods_name,shop.goods_introduct,shop.goods_ico,shop.member_price')
            ->where('zx_collection_product.uid', $uid)
            ->where('shop.status', 1)
            ->order('zx_collection_product.id desc')
            ->limit($start, self::$totalList)
            ->select();

        $totalCount = Db::table('zx_collection_product')->join('zx_shop shop', 'zx_collection_product.product_id = shop.id')
            ->where('zx_collection_product.uid', $uid)
            ->where('shop.status', 1)
            ->count();

        if ($collInfos) {
            foreach ($collInfos as &$v) {
                $v['goods_ico'] = get_cover($v['goods_ico'], 'path');

//                $sku = ProductSku::where('product_id', $v->product_id)->where('number', '>=', 1)->orderBy('member_price', 'asc')->first();
//                if ($sku) {
//                    $member_price = $sku->member_price;
//                    $parameter = $sku->value;
//                    $parameterId = $sku->id;
//                } else {
//                    $member_price = 0.00;
//                    $parameter = '无';
//                    $parameterId = 0;
//                }

//                $v->price = $member_price;
//                $v->parameter = $parameter;
//                $v->parameter_id = $parameterId;
            }
        }

        $this->apiSuccess('商品收藏列表', ['data' => $collInfos, 'totalCount' => $totalCount]);
    }

    /**
     * 删除收藏
     */
    public function delCollect()
    {
        $uid = $this->userinfo['uid'];
        $ids = input('ids');

        $idsArr = array_unique(explode(',', $ids));

        $res = Db('collection_product')->where('uid', $uid)->where('id', 'in', $idsArr)->delete();
        if ($res) {
            $this->apiSuccess('删除成功');
        } else {
            $this->apiError('删除失败');
        }

    }
}
